﻿@model RewardPointsSettingsModel

@{
    ViewBag.Title = T("Admin.Configuration.Settings.RewardPoints").Value;
    Layout = "_SettingLayout";
}

<form asp-action="RewardPoints">

    <div class="section-header">
        <div class="title">
            <i class="fa fa-trophy"></i>
            @T("Admin.Configuration.Settings.RewardPoints")
        </div>
        <div class="options">
            <button type="submit" name="save" value="save" class="btn btn-warning">
                <i class="fa fa-check"></i>
                <span>@T("Admin.Common.Save")</span>
            </button>
        </div>
    </div>

    @await Component.InvokeAsync("StoreScope")

    <div asp-validation-summary="All"></div>

    <div class="alert alert-info alert-dismissible">
        @T("Admin.Configuration.Settings.RewardPoints.Description")
        <button class="btn-close" data-dismiss="alert"></button>
    </div>

    <div class="adminContent">
        <div class="adminRow">
            <div class="adminTitle">
                <smart-label asp-for="Enabled" />
            </div>
            <div class="adminData">
                <setting-editor asp-for="Enabled" data-toggler-for="#pnlRewardPointSettings"></setting-editor>
                <span asp-validation-for="Enabled"></span>
            </div>
        </div>
        <div id="pnlRewardPointSettings">
            <div class="adminRow">
                <div class="adminTitle">
                    <smart-label asp-for="ExchangeRate" />
                </div>
                <div class="adminData">
                    <setting-editor asp-for="ExchangeRate">
                        <div class="container p-0">
                            <div class="row no-gutters">
                                <div class="input-group-prepend col-auto number-input-prepend">
                                    <span class="input-group-text">@T("Admin.Configuration.Settings.RewardPoints.ExchangeRate.Hint2")</span>
                                </div>
                                <div class="col">
                                    <editor asp-for="ExchangeRate" sm-postfix="@Model.PrimaryStoreCurrencyCode" />
                                </div>
                            </div>
                        </div>
                    </setting-editor>
                    <span asp-validation-for="ExchangeRate"></span>
                </div>
            </div>
            <div class="adminRow">
                <div class="adminTitle">
                    <smart-label asp-for="RoundDownRewardPoints" />
                </div>
                <div class="adminData">
                    <setting-editor asp-for="RoundDownRewardPoints"></setting-editor>
                    <span asp-validation-for="RoundDownRewardPoints"></span>
                </div>
            </div>
            <div class="adminRow">
                <div class="admin-config-group">
                    <div class="title">@T("Admin.Configuration.Settings.RewardPoints.Earning")</div>
                </div>
            </div>
            <div class="adminRow">
                <div class="adminTitle">
                    <smart-label asp-for="PointsForRegistration" />
                </div>
                <div class="adminData">
                    <setting-editor asp-for="PointsForRegistration"></setting-editor>
                    <span asp-validation-for="PointsForRegistration"></span>
                </div>
            </div>
            <div class="adminRow">
                <div class="adminTitle">
                    <smart-label asp-for="PointsForProductReview" />
                </div>
                <div class="adminData">
                    <setting-editor asp-for="PointsForProductReview"></setting-editor>
                    <span asp-validation-for="PointsForProductReview"></span>
                </div>
            </div>
            <div id="pnlPointsForProductReview" class="adminRow">
                <div class="adminTitle">
                    <smart-label asp-for="ShowPointsForProductReview" />
                </div>
                <div class="adminData">
                    <setting-editor asp-for="ShowPointsForProductReview"></setting-editor>
                    <span asp-validation-for="ShowPointsForProductReview"></span>
                </div>
            </div>
            <div class="adminRow">
                <div class="adminTitle">
                    <smart-label asp-for="PointsForPurchases_Points" />
                </div>
                <div class="adminData">
                    <div class="form-row flex-nowrap">
                        <div class="col w-50">
                            <setting-editor asp-for="PointsForPurchases_Amount" sm-postfix="@Model.PrimaryStoreCurrencyCode" placeholder="@T("RewardPoints.OrderAmount")"></setting-editor>
                            <span asp-validation-for="PointsForPurchases_Amount"></span>
                        </div>
                        <div class="col w-50">
                            <setting-editor asp-for="PointsForPurchases_Points" placeholder="@T("RewardPoints.Fields.Points")"></setting-editor>
                            <span asp-validation-for="PointsForPurchases_Points"></span>
                        </div>
                    </div>
                    <div id="PointsForPurchasesInfo" class="text-muted mt-1"></div>
                </div>
            </div>
            <div id="pnlPointsForProductPurchase" class="adminRow">
                <div class="adminTitle">
                    <smart-label asp-for="ShowPointsForProductPurchase" />
                </div>
                <div class="adminData">
                    <setting-editor asp-for="ShowPointsForProductPurchase"></setting-editor>
                    <span asp-validation-for="ShowPointsForProductPurchase"></span>
                </div>
            </div>
            <div class="adminRow">
                <div class="adminTitle">
                    <smart-label asp-for="PointsForPurchases_Awarded" />
                </div>
                <div class="adminData">
                    <setting-editor asp-for="PointsForPurchases_Awarded"></setting-editor>
                    <span asp-validation-for="PointsForPurchases_Awarded"></span>
                </div>
            </div>
            <div class="adminRow">
                <div class="adminTitle">
                    <smart-label asp-for="PointsForPurchases_Canceled" />
                </div>
                <div class="adminData">
                    <setting-editor asp-for="PointsForPurchases_Canceled"></setting-editor>
                    <span asp-validation-for="PointsForPurchases_Canceled"></span>
                </div>
            </div>
        </div>
    </div>
</form>

<script sm-target-zone="scripts" data-origin="rewardpoints-settings">
    $(function () {
        var pointsForPurchasesAmount = $('#@Html.IdFor(x => x.PointsForPurchases_Amount)');
        var pointsForPurchasesPoints = $('#@Html.IdFor(x => x.PointsForPurchases_Points)');

        pointsForPurchasesAmount.on('blur', getRewardPointsForPurchasesInfo);
        pointsForPurchasesPoints.on('change', getRewardPointsForPurchasesInfo).trigger('change');

        function getRewardPointsForPurchasesInfo() {
            var amount = pointsForPurchasesAmount.val();
            var points = pointsForPurchasesPoints.val();

            $.ajax({
                cache: false,
                global: false,
                type: 'GET',
                url: `@(Url.Action("RewardPointsForPurchasesInfo"))?amount=${amount}&points=${points}`,
                success: function (response) {
                    $('#PointsForPurchasesInfo').html(response);
                },
                error: function (objXml) {
                    if (objXml != null && objXml.responseText != null && objXml.responseText !== '') {
                        console.log(objXml.responseText);
                    }
                }
            });
        }

        $('#@Html.IdFor(x => x.PointsForProductReview)').on('change', function () {
            const points = parseInt($(this).val()) || 0;
            $('#pnlPointsForProductReview').toggle(points > 0);
        });

        $('#@Html.IdFor(x => x.PointsForPurchases_Points)').on('change', function () {
            const points = parseInt($(this).val()) || 0;
            $('#pnlPointsForProductPurchase').toggle(points > 0);
        });
    });
</script>